subroutine remove_object(sid,lid)
  use event_param
  use table_param,only:sim_obj
  use table_param,only:n_sim_obj
  use table_param,only:del_sim_obj
  use table_param,only:n_del_sim_obj
  use table_param,only:event_table
  use table_param,only:n_event_table
  use table_param,only:spont_event_table
  use table_param,only:n_spont_event_table
  use table_param,only:del_event_table
  use table_param,only:n_del_event_table
  use table_param,only:event_profile
  use label_data_param,only:obj_type
  use count_param,only:count_obj
  use count_param,only:total_count_obj
  use box_param,only:vbox
  implicit none

  integer,intent(in) :: sid,lid
  integer :: i
  integer :: gid,nx,ny,nz,bid
  integer :: eid,seid,eid2,pid,nid,prank,ne
  integer :: sid2,srank

  do i=1,sim_obj(sid)%n_event

     ! Index for event table
     eid=sim_obj(sid)%event_table_list(i)

     ! remove it from the event table
     n_del_event_table=n_del_event_table+1
     del_event_table(n_del_event_table)=eid
     event_table(eid)%removed=.true.
     n_event_table=n_event_table-1
     
     select case(event_table(eid)%event_type)
     case(diff_event)
        ! Update the spontaneous table or event profile
        if (event_table(eid)%is_spontaneous) then
           ! Update the spontaneous table
           seid=event_table(eid)%spont_table_index
           eid2=spont_event_table(n_spont_event_table)
           spont_event_table(seid)=eid2
           event_table(eid2)%spont_table_index=seid
           n_spont_event_table=n_spont_event_table-1
        else
           ! Update the event profile
           pid=event_table(eid)%profile_table_index
           prank=event_table(eid)%profile_table_seq
           ne=event_profile(pid)%n_event
           eid2=event_profile(pid)%event_list(ne)
           event_profile(pid)%event_list(prank)=eid2
           event_table(eid2)%profile_table_seq=prank
           ne=ne-1
           event_profile(pid)%n_event=ne
        end if
     case(diss_event)
        ! Update the spontaneous table or event profile
        if (event_table(eid)%is_spontaneous) then
           ! Update the spontaneous table
           seid=event_table(eid)%spont_table_index
           eid2=spont_event_table(n_spont_event_table)
           spont_event_table(seid)=eid2
           event_table(eid2)%spont_table_index=seid
           n_spont_event_table=n_spont_event_table-1
        else
           ! Update the event profile
           pid=event_table(eid)%profile_table_index
           prank=event_table(eid)%profile_table_seq
           ne=event_profile(pid)%n_event
           eid2=event_profile(pid)%event_list(ne)
           event_profile(pid)%event_list(prank)=eid2
           event_table(eid2)%profile_table_seq=prank
           ne=ne-1
           event_profile(pid)%n_event=ne
        end if
     case(rctn_event)
        ! Adjust the event table for the other object
        if (sid.ne.event_table(eid)%obj1) then
           sid2=event_table(eid)%obj1
           srank=event_table(eid)%obj_seq1
           ne=sim_obj(sid2)%n_event
        else
           sid2=event_table(eid)%obj2
           srank=event_table(eid)%obj_seq2
           ne=sim_obj(sid2)%n_event
        end if
        eid2=sim_obj(sid2)%event_table_list(ne)
        sim_obj(sid2)%event_table_list(srank)=eid2
        ne=ne-1
        sim_obj(sid2)%n_event=ne
        if (sid2.eq.event_table(eid2)%obj1) then
           event_table(eid2)%obj_seq1=srank
        else
           event_table(eid2)%obj_seq2=srank
        end if

        ! Update the spontaneous table or event profile
        if (event_table(eid)%is_spontaneous) then
           ! Update the spontaneous table
           seid=event_table(eid)%spont_table_index
           eid2=spont_event_table(n_spont_event_table)
           spont_event_table(seid)=eid2
           event_table(eid2)%spont_table_index=seid
           n_spont_event_table=n_spont_event_table-1
        else
           ! Update the event profile
           pid=event_table(eid)%profile_table_index
           prank=event_table(eid)%profile_table_seq
           ne=event_profile(pid)%n_event
           eid2=event_profile(pid)%event_list(ne)
           event_profile(pid)%event_list(prank)=eid2
           event_table(eid2)%profile_table_seq=prank
           ne=ne-1
           event_profile(pid)%n_event=ne
        end if
     case(extn_event)
     case(sink_event)
        ! Update the spontaneous table or event profile
        if (event_table(eid)%is_spontaneous) then
           ! Update the spontaneous table
           seid=event_table(eid)%spont_table_index
           eid2=spont_event_table(n_spont_event_table)
           spont_event_table(seid)=eid2
           event_table(eid2)%spont_table_index=seid
           n_spont_event_table=n_spont_event_table-1
        else
           ! Update the event profile
           pid=event_table(eid)%profile_table_index
           prank=event_table(eid)%profile_table_seq
           ne=event_profile(pid)%n_event
           eid2=event_profile(pid)%event_list(ne)
           event_profile(pid)%event_list(prank)=eid2
           event_table(eid2)%profile_table_seq=prank
           ne=ne-1
           event_profile(pid)%n_event=ne
        end if
     case(srce_event)
     end select
  end do

  ! remove sim_obj
  sim_obj(sid)%removed=.true.
  sim_obj(sid)%n_event=0
  n_del_sim_obj=n_del_sim_obj+1
  del_sim_obj(n_del_sim_obj)=sid
  n_sim_obj=n_sim_obj-1

  ! Adjust the counting
  nx=sim_obj(sid)%box(1)
  ny=sim_obj(sid)%box(2)
  nz=sim_obj(sid)%box(3)

  do i=1,obj_type(lid)%n_group_belong
     gid=obj_type(lid)%group_list(i)
     nid=obj_type(lid)%config(count_obj(gid)%point_type)
     if (count_obj(gid)%group_type.lt.3) then
        count_obj(gid)%nx(nx)=count_obj(gid)%nx(nx)-1
        count_obj(gid)%ny(ny)=count_obj(gid)%ny(ny)-1
        count_obj(gid)%nz(nz)=count_obj(gid)%nz(nz)-1
        total_count_obj(gid)=total_count_obj(gid)-1
     else
        count_obj(gid)%nx(nx)=count_obj(gid)%nx(nx)-nid
        count_obj(gid)%ny(ny)=count_obj(gid)%ny(ny)-nid
        count_obj(gid)%nz(nz)=count_obj(gid)%nz(nz)-nid
        total_count_obj(gid)=total_count_obj(gid)-nid
     end if
  end do

  ! Adjust the virtual box
  bid=sim_obj(sid)%vbox_index
  if (sim_obj(sid)%prev.eq.sid) then
     gid=sim_obj(sid)%next
     vbox(bid)%first=gid
     sim_obj(gid)%prev=gid
  else if (sim_obj(sid)%next.eq.sid) then
     gid=sim_obj(sid)%prev
     sim_obj(gid)%next=gid
  else
     sim_obj(sim_obj(sid)%prev)%next=sim_obj(sid)%next
     sim_obj(sim_obj(sid)%next)%prev=sim_obj(sid)%prev
  end if
  vbox(bid)%n_obj=vbox(bid)%n_obj-1

  return

end subroutine remove_object
